$('document').ready(function () {
    // 加载页面时触发的事件
    if ($(window).width() > 1280) {
        $('#upade-modal').css('margin-right', '33%')
    } else if ($(window).width() < 1000) {
        $('#upade-modal').css('margin-right', '13%')
    }
    if (parseInt(getCookie('code')) !== 201) {
        window.location.href = 'home.html'
    }

    // 获取公开课课程列表
    var totalPageNum = 0
    $.ajax({
        type: 'GET',
        url: 'http://111.229.120.67/tea/pub-course-list',
        data: {
            "pageNum": 1,
            "pageSize": 30
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.statusCode == 0) {
                // console.log(data)
                totalPageNum = data.page.totalPageNum
                if (totalPageNum > 1) {
                    for (var i = 2; i < totalPageNum + 1; i++) {
                        $('#raquo').before(`
                        <li id="page` + i + `"><a href="javascript:void(0)">` + i + `</a></li>
                        `)
                    }
                } else {
                    $('#pagination').css('display', 'none')
                }
                var array = data.page.pubcourseList
                for (var i = 0; i < array.length; i++) {
                    $('#list-tbody').append(`
                        <tr>
                            <td>` + array[i].id + `</td>
                            <td>` + array[i].name + `</td>
                            <td>` + array[i].ispublic + `</td>
                            <td>
                                <a href="javascript:void(0)" class="course-watch">查看</a>                
                            </td>
                            <td>
                                             
                            </td>
                            <td>
                                           
                            </td>
                        </tr>
                    `)
                }
            } else {
                $('#myModal-public').modal({})
                $('#myModal-public h4').html('请求超时')
                $('#myModal-public-body').html('请求超时，请重试')
            }
        }
    })
    // 分页
    $('#pagination').on('mouseover', function () {
        $('#pagination li').off('click').click(function () {
            $('#pagination li').removeClass("active")
            $('#pagination li').eq($(this).text()).addClass("active")
            // console.log($(this).text())
            // console.log($('#pagination .active').text())
            if ($('#pagination .active').text() == '1') {
                $('#laquo').css('display', 'none')
            } else {
                $('#laquo').css('display', 'inline')
            }
            if ($('#pagination .active').text() == totalPageNum) {
                $('#raquo').css('display', 'none')
            } else {
                $('#raquo').css('display', 'inline')
            }
            if (Mycourse == 1) {
                $.ajax({
                    type: 'GET',
                    url: 'http://111.229.120.67/tea/tch-courses',
                    data: {
                        "pageNum": parseInt($(this).text()),
                        "pageSize": 30
                    },
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (data) {
                        // console.log(data)
                        if (data.statusCode == 0) {
                            var array = data.page.list
                            for (var i = 0; i < array.length; i++) {
                                $('#list-tbody').append(`
                                    <tr>
                                    <td>` + array[i].id + `</td>
                                    <td>` + array[i].name + `</td>
                                    <td>` + array[i].ispublic + `</td>
                                    <td>
                                        <a href="javascript:void(0)" class="course-watch">查看</a>                
                                    </td>
                                    <td>
                                        <a href="javascript:void(0)" class="course-upade">修改</a>                
                                    </td>
                                    <td>
                                        <a href="javascript:void(0)" class="course-remove">删除</a>                
                                    </td>
                                    </tr>
                                `)
                            }
                        } else {
                            $('#myModal-public').modal({})
                            $('#myModal-public h4').html('请求超时')
                            $('#myModal-public-body').html('请求超时，请重试')
                        }
                    }
                })
            } else {
                $.ajax({
                    type: 'GET',
                    url: 'http://111.229.120.67/tea/pub-course-list',
                    data: {
                        "pageNum": parseInt($(this).text()),
                        "pageSize": 30
                    },
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (data) {
                        // console.log(data)
                        if (data.statusCode == 0) {
                            $('#list-tbody').html('')
                            var array = data.page.pubcourseList
                            for (var i = 0; i < array.length; i++) {
                                $('#list-tbody').append(`
                                    <tr>
                                        <td>` + array[i].id + `</td>
                                        <td>` + array[i].name + `</td>
                                        <td>` + array[i].ispublic + `</td>
                                        <td>
                                            <a href="javascript:void(0)" class="course-watch">查看</a>                
                                        </td>
                                        <td>
                                                         
                                        </td>
                                        <td>
                                                       
                                        </td>
                                    </tr>
                                `)
                            }
                        } else {
                            $('#myModal-public').modal({})
                            $('#myModal-public h4').html('请求超时')
                            $('#myModal-public-body').html('请求超时，请重试')
                        }
                    }
                })
            }
        })
        $('#laquo').off('click').click(function () {
            var active = $('#pagination .active').text()
            var laquo = $('#pagination li').eq(active - 1).text()
            $('#pagination .active').removeClass("active")
            $('#pagination li').eq(laquo).addClass("active")
            if (laquo == 1) {
                $('#laquo').css('display', 'none')
            }
            if ($('#pagination .active').text() !== totalPageNum) {
                $('#raquo').css('display', 'inline')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/pub-course-list',
                data: {
                    "pageNum": parseInt($('#pagination li').eq(laquo).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#page1').text(data.prePageNum)
                        $('#page2').text(data.pageNum)
                        $('#page3').text(data.nextPageNum)
                        $('#list-tbody').html('')
                        var array = data.page.pubcourseList
                        for (var i = 0; i < array.length; i++) {
                            $('#list-tbody').append(`
                                <tr>
                                    <td>` + array[i].id + `</td>
                                    <td>` + array[i].name + `</td>
                                    <td>` + array[i].ispublic + `</td>
                                    <td>
                                        <a href="javascript:void(0)" class="course-watch">查看</a>                
                                    </td>
                                    <td>
                                                     
                                    </td>
                                    <td>
                                                   
                                    </td>
                                </tr>
                            `)
                        }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        $('#raquo').off('click').click(function () {
            var active = $('#pagination .active').text()
            var raquo = $('#pagination li').eq(parseInt(active) + 1).text()
            $('#pagination .active').removeClass("active")
            $('#pagination li').eq(raquo).addClass("active")
            $('#laquo').css('display', 'inline')
            if ($('#pagination .active').text() == totalPageNum) {
                $('#raquo').css('display', 'none')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/pub-course-list',
                data: {
                    "pageNum": parseInt($('#pagination li').eq(raquo).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    // console.log(data)
                    if (data.statusCode == 0) {
                        $('#page1').text(data.prePageNum)
                        $('#page2').text(data.pageNum)
                        $('#page3').text(data.nextPageNum)
                        $('#list-tbody').html('')
                        var array = data.page.pubcourseList
                        for (var i = 0; i < array.length; i++) {
                            $('#list-tbody').append(`
                                <tr>
                                    <td>` + array[i].id + `</td>
                                    <td>` + array[i].name + `</td>
                                    <td>` + array[i].ispublic + `</td>
                                    <td>
                                        <a href="javascript:void(0)" class="course-watch">查看</a>                
                                    </td>
                                    <td>
                                                     
                                    </td>
                                    <td>
                                                   
                                    </td>
                                </tr>
                            `)
                        }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
    })
    // -------------------------------------------

    // 查看自己负责的课程
    var Mycourse = 0
    $('#mycourse').click(function () {
        $.ajax({
            type: 'GET',
            url: 'http://111.229.120.67/tea/tch-courses',
            data: {
                "pageNum": 1,
                "pageSize": 15
            },
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                // console.log(data)
                Mycourse = 1
                if (data.statusCode == 0) {
                    var array = data.page.list
                    $('#list-tbody').html('')
                    for (var i = 0; i < array.length; i++) {
                        $('#list-tbody').append(`
                            <tr>
                            <td>` + array[i].id + `</td>
                            <td>` + array[i].name + `</td>
                            <td>` + array[i].ispublic + `</td>
                            <td>
                                <a href="javascript:void(0)" class="course-watch">查看</a>                
                            </td>
                            <td>
                                <a href="javascript:void(0)" class="course-upade">修改</a>                
                            </td>
                            <td>
                                <a href="javascript:void(0)" class="course-remove">删除</a>                
                            </td>
                            </tr>
                        `)
                    }
                } else if (data.statusCode == -1) {
                    $('#myModal-public').modal({})
                    $('#myModal-public h4').html('您还没添加课程')
                    $('#myModal-public-body').html('暂无负责的课程')
                } else {
                    $('#myModal-public').modal({})
                    $('#myModal-public h4').html('请求超时')
                    $('#myModal-public-body').html('请求超时，请重试')
                }
            }
        })
    })

    // 接收的大纲和教材信息全局变量
    var outlines_array = []
    var coursesourcebook_array = []
    var courseid_all = 0
    var lasta_value = 0
    var lasta_value1 = 0
    var remove_courseid = 0 //课程删除按钮获取课程id
    $('#table-responsive tbody').on('mouseenter', function () {
        // 课程列表-查看课程详情
        $('.course-watch').off('click').click(function () {
            var index = $('.course-watch').index($(this)[0])
            var courseid = $('#table-responsive tbody tr:eq(' + index + ') td').eq(0).html()
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/course',
                data: {
                    "id": courseid
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        outlines_array = data.outlines
                        coursesourcebook_array = data.textbooks
                        $('#myModal-public').css('top', 0)
                        $('#myModal-public h4').html('查看课程信息(ID:' + data.id + ')')
                        $('#myModal-public-body').html(`
                            <div id="upade-modal">
                            名称：<span id="coursename">` + data.name + `</span>
                            <br>
                            <br>
                            类别：<span id="coursetype">` + data.ispublic + `</span>
                            <br>
                            <br>
                            大纲：<span id="courseoutline"> ` + data.outlines[0].sectionName + `（点击查看详情）</span>
                            <br>
                            <br>
                            教材：<span id="coursesourcebook">` + data.textbooks[0].name + `（点击查看详情）</span>
                            </div>
                        `)
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                        )
                        $('#myModal-public').modal({

                        })
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })

        // 课程列表-课程删除

        $('.course-remove').click(function () {
            var remove_index = $('.course-remove').index($(this)[0])
            var courseid = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(0).html()
            remove_courseid = courseid
            // console.log(remove_courseid)
            $('#myModal-public h4').html('删除ID(' + courseid + ')课程')
            $('#myModal-public-body').html('该操作会将该课程信息删除')
            $('#myModal-public .modal-footer').html(`
            <button type="button" id="sureremove" class="btn btn-primary">确认删除</button>
            </button><button type="button" class="btn" data-dismiss="modal">关闭</button>
            `)
            $('#myModal-public').modal({

            })
        })

        // 课程列表-课程修改
        $('.course-upade').click(function () {
            var index = $('.course-upade').index($(this)[0])
            var courseid = $('#table-responsive tbody tr:eq(' + index + ') td').eq(0).html()
            courseid_all = courseid
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/course',
                data: {
                    "id": courseid
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        outlines_array = data.outlines
                        coursesourcebook_array = data.textbooks
                        $('#myModal-public').css('top', 0)
                        $('#myModal-public h4').html('修改课程信息(ID:' + data.id + ')')
                        $('#myModal-public-body').html(`
                            <div id="upade-modal">
                            名称：<input id="coursename" value="` + data.name + `">
                            <br>
                            <br>
                            类别：<input id="coursetype" value="` + data.ispublic + `">
                            <br>
                            <br>
                            大纲：<input id="courseoutline-update" value="` + data.outlines[0].sectionName + `（点击查看详情）" readonly>
                            <br>
                            <br>
                            教材：<input id="coursesourcebook-update" value="` + data.textbooks[0].name + `（点击查看详情）" readonly>
                            </div>
                        `)

                        $('#myModal-public .modal-footer').html(
                            `<button type="button" id="surebtn" class="btn btn-primary">确认修改</button>
                             <button type="button" class="btn" data-dismiss="modal">关闭</button>
                            `
                        )
                        $('#myModal-public').modal({
                            backdrop: 'static',
                            keyboard: false
                        })
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
    })

    // 课程 or 教材
    var outlines_status = 0 //只有一个大纲时点继续添加
    var remove_outline_id = 0 //删除大纲
    var coursesourcebook_status = 0 //只有一个教材时点继续添加
    var remove_coursesourcebook_isbn = 0 //删除教材
    $('#myModal-public').on('mouseenter', function () {
        $('#outline-add').off("click").click(function () {
            if ($('#myModal-public-body a').size() < 1) {
                outlines_status = 1
                $('#myModal-public-body').append(`
                        <p style="color:"blue";">< ` + parseInt(outlines_status + 1) + ` ></p>
                    <a href="javascript:void(0)" style="display:none" id="zjremove` + outlines_status + `" value="` + outlines_status + `" class="iconfont icon-jian zjremove"></a>
                    <p id="outline-name` + outlines_status + `">大纲名：<input type="text"></p>
                    <p id="outline-sectionNum` + outlines_status + `">章节号：<input type="text"></p>
                    <p id="outline-sectionName` + outlines_status + `">章节名：<input type="text"></p>
                    `)
                $('#myModal-public .modal-footer').html(
                    `
                <button type="button" class="btn btn-primary" id="surebtn1">确定</button>
                <button type="button" class="btn btn-primary" id="outline-add">继续添加</button>
                <button type="button" class="btn" data-dismiss="modal">关闭</button>
                `
                )
            } else {
                lasta_value = parseInt($('#myModal-public-body a:last').attr('value'))
                lasta_value++
                $('#myModal-public-body').append(`
            <p style="color:"blue";">< ` + parseInt(lasta_value + 1) + ` ></p>
        <a href="javascript:void(0)" style="display:none" id="zjremove` + lasta_value + `" value="` + lasta_value + `" class="iconfont icon-jian zjremove"></a>
        <p id="outline-name` + lasta_value + `">大纲名：<input type="text"></p>
        <p id="outline-sectionNum` + lasta_value + `">章节号：<input type="text"></p>
        <p id="outline-sectionName` + lasta_value + `">章节名：<input type="text"></p>
        `)
                $('#myModal-public .modal-footer').html(
                    `
            <button type="button" class="btn btn-primary" id="surebtn1">确定</button>
            <button type="button" class="btn btn-primary" id="outline-add">继续添加</button>
            <button type="button" class="btn" data-dismiss="modal">关闭</button>
            `
                )
            }

        })
        // 查看功能里的大纲点击事件
        $('#courseoutline').off('click').click(function () {
            $('#myModal-public-body').html(`
            <p style="color:"blue";">< ` + 1 + ` ></p>
            大纲名：<span>` + outlines_array[0].name + `</span>
            <br>
            <br>
            章节号：<span>` + outlines_array[0].sectionNum + `</span>
            <br>
            <br>
            章节名：<span>` + outlines_array[0].sectionName + `</span>
            <br>
            `)
            for (var i = 1; i < outlines_array.length; i++) {
                $('#myModal-public-body').append(`
                <br>
                <p style="color:"blue";">< ` + parseInt(i + 1) + ` ></p>
                大纲名：<span>` + outlines_array[i].name + `</span>
                <br>
                <br>
                章节号：<span>` + outlines_array[i].sectionNum + `</span>
                <br>
                <br>
                章节名：<span>` + outlines_array[i].sectionName + `</span>
                <br>
            `)
            }

        })
        // 修改功能里的大纲点击事件
        $('#courseoutline-update').click(function () {
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/course',
                data: {
                    "id": courseid_all
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    // console.log(data)
                    if (data.statusCode == 0) {
                        outlines_array = data.outlines
                        $('#myModal-public-body').html(`
                            <p id="outline-id" style="color:"blue";" value="` + data.outlines[0].id + `">< ` + 1 + ` ></p>
                            大纲名：<input type="text" id="outline-name" value="` + outlines_array[0].name + `" placeholder="不填写表示该项保留原状">
                            <br>
                            <br>
                            章节号：<input type="text" id="outline-sectionNum" value="` + outlines_array[0].sectionNum + `" placeholder="不填写表示该项保留原状">
                            <br>
                            <br>
                            章节名：<input type="text" id="outline-sectionName" value="` + outlines_array[0].sectionName + `" placeholder="不填写表示该项保留原状">
                            <br>
                            `)
                        for (var i = 1; i < outlines_array.length; i++) {
                            $('#myModal-public-body').append(`
                                <br>
                                <p id="outline-id` + i + `" style="color:"blue"; value="` + data.outlines[i].id + `">< ` + parseInt(i + 1) + ` ><a href="javascript:void(0)" id="zjremove` + i + `" value="` + i + `" class="iconfont icon-jian zjremove"></a></p>
                                <p id="outline-name` + i + `">大纲名：<input type="text" value="` + outlines_array[i].name + `" placeholder="不填写表示该项保留原状"></p>
                                <p id="outline-sectionNum` + i + `">章节号：<input type="text" value="` + outlines_array[i].sectionNum + `" placeholder="不填写表示该项保留原状"></p>
                                <p id="outline-sectionName` + i + `">章节名：<input type="text" value="` + outlines_array[i].sectionName + `" placeholder="不填写表示该项保留原状"></p>
                            `)
                        }
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" id="surebtn1" class="btn btn-primary">确认修改</button>
                            <button type="button" class="btn btn-primary" id="outline-add">继续添加</button>
                             <button type="button" class="btn" data-dismiss="modal">关闭</button>
                            `
                        )
                        $('#myModal-public').modal({

                        })
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        // 删除大纲
        $('.zjremove').off("click").click(function () {
            var index = $(this).attr('value')
            remove_outline_id = $('#outline-id' + index + '').attr('value')
            $('#myModal-public-body').html(`
                <p>是否删除大纲：` + outlines_array[index].name + `？</p>
                <button id="outline-remove-ture">是</button>
                <button id="outline-remove-false">否</button>
            `)
            $('#myModal-public .modal-footer').html('')
        })
        // 确认删除大纲
        $('#outline-remove-ture').off("click").click(function () {
            // console.log(remove_outline_id)
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/del-outline',
                data: {
                    "id": remove_outline_id
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $.ajax({
                            type: 'GET',
                            url: 'http://111.229.120.67/tea/course',
                            data: {
                                "id": courseid_all
                            },
                            xhrFields: {
                                withCredentials: true
                            },
                            success: function (data) {
                                if (data.statusCode == 0) {
                                    outlines_array = data.outlines
                                    coursesourcebook_array = data.textbooks
                                    $('#myModal-public-body').html(`
                                        <p id="outline-id" style="color:"blue";" value="` + outlines_array[0].id + `">< ` + 1 + ` ></p>
                                        大纲名：<input type="text" id="outline-name" value="` + outlines_array[0].name + `" placeholder="不填写表示该项保留原状">
                                        <br>
                                        <br>
                                        章节号：<input type="text" id="outline-sectionNum" value="` + outlines_array[0].sectionNum + `" placeholder="不填写表示该项保留原状">
                                        <br>
                                        <br>
                                        章节名：<input type="text" id="outline-sectionName" value="` + outlines_array[0].sectionName + `" placeholder="不填写表示该项保留原状">
                                        <br>
                                    `)
                                    for (var i = 1; i < outlines_array.length; i++) {
                                        $('#myModal-public-body').append(`
                                            <br>
                                            <p id="outline-id` + i + `" style="color:"blue";" value="` + outlines_array[i].id + `">< ` + parseInt(i + 1) + ` ><a href="javascript:void(0)" id="zjremove` + parseInt(i + 1) + `" value="` + parseInt(i + 1) + `" class="iconfont icon-jian zjremove"></a></p>
                                            <p id="outline-name` + i + `">大纲名：<input type="text" value="` + outlines_array[i].name + `" placeholder="不填写表示该项保留原状"></p>
                                            <p id="outline-sectionNum` + i + `">章节号：<input type="text" value="` + outlines_array[i].sectionNum + `" placeholder="不填写表示该项保留原状"></p>
                                            <p id="outline-sectionName` + i + `">章节名：<input type="text" value="` + outlines_array[i].sectionName + `" placeholder="不填写表示该项保留原状"></p>
                                        `)
                                    }
                                    $('#myModal-public .modal-footer').html(
                                        `<button type="button" id="surebtn1" class="btn btn-primary">确认修改</button>
                                            <button type="button" class="btn" data-dismiss="modal">关闭</button>
                                            `
                                    )
                                    $('#myModal-public').modal({

                                    })
                                }
                            }
                        })
                    } else {
                        $('#myModal-public h4').html('删除失败')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        $('#outline-remove-false').off("click").click(function () {
            $('#myModal-public-body').html(`
                <p id="outline-id" style="color:"blue";" value="` + outlines_array[0].id + `">< ` + 1 + ` ></p>
                大纲名：<input type="text" id="outline-name" value="` + outlines_array[0].name + `" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                章节号：<input type="text" id="outline-sectionNum" value="` + outlines_array[0].sectionNum + `" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                章节名：<input type="text" id="outline-sectionName" value="` + outlines_array[0].sectionName + `" placeholder="不填写表示该项保留原状">
                <br>
                `)
            for (var i = 1; i < outlines_array.length; i++) {
                $('#myModal-public-body').append(`
                    <br>
                    <p id="outline-id` + i + `" style="color:"blue";" value="` + outlines_array[i].id + `">< ` + parseInt(i + 1) + ` ><a href="javascript:void(0)" id="zjremove` + i + `" value="` + i + `" class="iconfont icon-jian zjremove"></a></p>
                    <p id="outline-name` + i + `">大纲名：<input type="text" value="` + outlines_array[i].name + `" placeholder="不填写表示该项保留原状"></p>
                    <p id="outline-sectionNum` + i + `">章节号：<input type="text" value="` + outlines_array[i].sectionNum + `" placeholder="不填写表示该项保留原状"></p>
                    <p id="outline-sectionName` + i + `">章节名：<input type="text" value="` + outlines_array[i].sectionName + `" placeholder="不填写表示该项保留原状"></p>
                `)
            }
            $('#myModal-public .modal-footer').html(
                `<button type="button" id="surebtn1" class="btn btn-primary">确认修改</button>
                    <button type="button" class="btn" data-dismiss="modal">关闭</button>
                `
            )
        })
        // ---------------------------------------------------------------
        $('#coursesourcebook-add').off("click").click(function () {
            if ($('#myModal-public-body a').size() < 1) {
                coursesourcebook_status = 1
                $('#myModal-public-body').append(`
                <p style="color:"blue";">< ` + parseInt(coursesourcebook_status + 1) + ` ></p>
                <a href="javascript:void(0)" style="display:none" id="jcremove` + coursesourcebook_status + `" value="` + lasta_value1 + `" class="iconfont icon-jian jcremove"></a>
                <p id="coursesourcebook-isbn` + coursesourcebook_status + `">书号：<input type="text"></p>
                <p id="coursesourcebook-name` + coursesourcebook_status + `">书名：<input type="text"></p>
                <p id="coursesourcebook-author` + coursesourcebook_status + `">作者：<input type="text"></p>
                <p id="coursesourcebook-press` + coursesourcebook_status + `">出版社：<input type="text"></p>
            `)
            } else {
                lasta_value1 = parseInt($('#myModal-public-body a:last').attr('value'))
                lasta_value1++
                $('#myModal-public-body').append(`
                <p style="color:"blue";">< ` + parseInt(lasta_value1 + 1) + ` ></p>
                <a href="javascript:void(0)" style="display:none" id="jcremove` + lasta_value1 + `" value="` + lasta_value1 + `" class="iconfont icon-jian jcremove"></a>
                <p id="coursesourcebook-isbn` + lasta_value1 + `">书号：<input type="text"></p>
                <p id="coursesourcebook-name` + lasta_value1 + `">书名：<input type="text"></p>
                <p id="coursesourcebook-author` + lasta_value1 + `">作者：<input type="text"></p>
                <p id="coursesourcebook-press` + lasta_value1 + `">出版社：<input type="text"></p>
            `)
                $('#myModal-public .modal-footer').html(
                    `
                <button type="button" class="btn btn-primary" id="surebtn2">确定</button>
                <button type="button" class="btn btn-primary" id="coursesourcebook-add">继续添加</button>
                <button type="button" class="btn" data-dismiss="modal">关闭</button>
                `
                )
            }

        })
        // 查看功能里的教材点击事件
        $('#coursesourcebook').off('click').click(function () {
            $('#myModal-public-body').html(`
            <p style="color:"blue";">< ` + 1 + ` ></p>
            书号：<span>` + coursesourcebook_array[0].isbn + `</span>
            <br>
            <br>
            书名：<span>` + coursesourcebook_array[0].name + `</span>
            <br>
            <br>
            作者：<span>` + coursesourcebook_array[0].author + `</span>
            <br>
            <br>
            出版社：<span>` + coursesourcebook_array[0].press + `</span>
            <br>
            `)
            for (var i = 1; i < coursesourcebook_array.length; i++) {
                $('#myModal-public-body').append(`
                <br>
                <p style="color:"blue";">< ` + parseInt(i + 1) + ` ></p>
                书号：<span>` + coursesourcebook_array[i].isbn + `</span>
                <br>
                <br>
                书名：<span>` + coursesourcebook_array[i].name + `</span>
                <br>
                <br>
                作者：<span>` + coursesourcebook_array[i].author + `</span>
                <br>
                <br>
                出版社：<span>` + coursesourcebook_array[i].press + `</span>
                <br>
            `)
            }
        })
        // 修改功能里的教材点击事件
        $('#coursesourcebook-update').off('click').click(function () {
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/course',
                data: {
                    "id": courseid_all
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        coursesourcebook_array = data.textbooks
                        $('#myModal-public-body').html(`
                            <p style="color:"blue";">< ` + 1 + ` ></p>
                            书号：<input type="text" id="coursesourcebook-isbn" value="` + coursesourcebook_array[0].isbn + `" placeholder="不填写表示该项保留原状">
                            <br>
                            <br>
                            书名：<input type="text" id="coursesourcebook-name" value="` + coursesourcebook_array[0].name + `" placeholder="不填写表示该项保留原状">
                            <br>
                            <br>
                            作者：<input type="text" id="coursesourcebook-author" value="` + coursesourcebook_array[0].author + `" placeholder="不填写表示该项保留原状">
                            <br>
                            <br>
                            出版社：<input type="text" id="coursesourcebook-press" value="` + coursesourcebook_array[0].press + `" placeholder="不填写表示该项保留原状">
                            <br>
                            `)
                        for (var i = 1; i < coursesourcebook_array.length; i++) {
                            $('#myModal-public-body').append(`
                                <br>
                                <p  style="color:"blue";" >< ` + parseInt(i + 1) + ` ><a href="javascript:void(0)" id="jcremove` + i + `" value="` + i + `" class="iconfont icon-jian jcremove"></a></p>
                                <p id="coursesourcebook-isbn` + i + `">书号：<input type="text" value="` + coursesourcebook_array[i].isbn + `" placeholder="不填写表示该项保留原状"></p>
                                <p id="coursesourcebook-name` + i + `">书名：<input type="text" value="` + coursesourcebook_array[i].name + `" placeholder="不填写表示该项保留原状"></p>
                                <p id="coursesourcebook-author` + i + `">作者：<input type="text" value="` + coursesourcebook_array[i].author + `" placeholder="不填写表示该项保留原状"></p>
                                <p id="coursesourcebook-press` + i + `">出版社：<input type="text" value="` + coursesourcebook_array[i].press + `" placeholder="不填写表示该项保留原状"></p>                
                            `)
                        }
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" id="surebtn2" class="btn btn-primary">确认修改</button>
                            <button type="button" class="btn btn-primary" id="coursesourcebook-add">继续添加</button>
                             <button type="button" class="btn" data-dismiss="modal">关闭</button>
                            `
                        )
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        // 删除教材
        $('.jcremove').off("click").click(function () {
            var index = $(this).attr('value')
            remove_coursesourcebook_isbn = $('#coursesourcebook-isbn' + index + ' input').val()
            $('#myModal-public-body').html(`
                <p>是否删除教材：` + coursesourcebook_array[index].name + `？</p>
                <button id="coursesourcebook-remove-ture">是</button>
                <button id="coursesourcebook-remove-false">否</button>
            `)
            $('#myModal-public .modal-footer').html('')
        })
        // 确认删除教材
        $('#coursesourcebook-remove-ture').off("click").click(function () {
            var index = $(this).attr('value')
            // console.log(remove_coursesourcebook_isbn)
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/del-tebook',
                data: {
                    "isbn": remove_coursesourcebook_isbn
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $.ajax({
                            type: 'GET',
                            url: 'http://111.229.120.67/tea/course',
                            data: {
                                "id": courseid_all
                            },
                            xhrFields: {
                                withCredentials: true
                            },
                            success: function (data) {
                                if (data.statusCode == 0) {
                                    coursesourcebook_array = data.textbooks
                                    $('#myModal-public-body').html(`
                                        <p style="color:"blue";">< ` + 1 + ` ></p>
                                        书号：<input type="text" id="coursesourcebook-isbn" value="` + coursesourcebook_array[0].isbn + `" placeholder="不填写表示该项保留原状">
                                        <br>
                                        <br>
                                        书名：<input type="text" id="coursesourcebook-name" value="` + coursesourcebook_array[0].name + `" placeholder="不填写表示该项保留原状">
                                        <br>
                                        <br>
                                        作者：<input type="text" id="coursesourcebook-author" value="` + coursesourcebook_array[0].author + `" placeholder="不填写表示该项保留原状">
                                        <br>
                                        <br>
                                        出版社：<input type="text" id="coursesourcebook-press" value="` + coursesourcebook_array[0].press + `" placeholder="不填写表示该项保留原状">
                                        <br>
                                        `)
                                    for (var i = 1; i < coursesourcebook_array.length; i++) {
                                        $('#myModal-public-body').append(`
                                            <br>
                                            <p  style="color:"blue";" >< ` + parseInt(i + 1) + ` ><a href="javascript:void(0)" id="jcremove` + i + `" value="` + i + `" class="iconfont icon-jian jcremove"></a></p>
                                            <p id="coursesourcebook-isbn` + i + `">书号：<input type="text" value="` + coursesourcebook_array[i].isbn + `" placeholder="不填写表示该项保留原状"></p>
                                            <p id="coursesourcebook-name` + i + `">书名：<input type="text" value="` + coursesourcebook_array[i].name + `" placeholder="不填写表示该项保留原状"></p>
                                            <p id="coursesourcebook-author` + i + `">作者：<input type="text" value="` + coursesourcebook_array[i].author + `" placeholder="不填写表示该项保留原状"></p>
                                            <p id="coursesourcebook-press` + i + `">出版社：<input type="text" value="` + coursesourcebook_array[i].press + `" placeholder="不填写表示该项保留原状"></p>                
                                        `)
                                    }
                                    $('#myModal-public .modal-footer').html(
                                        `<button type="button" id="surebtn2" class="btn btn-primary">确认修改</button>
                                        <button type="button" class="btn btn-primary" id="coursesourcebook-add">继续添加</button>
                                         <button type="button" class="btn" data-dismiss="modal">关闭</button>
                                        `
                                    )
                                } else {
                                    $('#myModal-public h4').html('请求超时')
                                    $('#myModal-public-body').html('请求超时，请重试')
                                }
                            }
                        })
                    } else {
                        $('#myModal-public h4').html('删除失败')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        $('#coursesourcebook-remove-false').off("click").click(function () {
            $('#myModal-public-body').html(`
            <p  style="color:"blue";" >< ` + 1 + ` ></p>
            书号：<input type="text" id="coursesourcebook-isbn" value="` + coursesourcebook_array[0].isbn + `" placeholder="不填写表示该项保留原状">
            <br>
            <br>
            书名：<input type="text" id="coursesourcebook-name" value="` + coursesourcebook_array[0].name + `" placeholder="不填写表示该项保留原状">
            <br>
            <br>
            作者：<input type="text" id="coursesourcebook-author" value="` + coursesourcebook_array[0].author + `" placeholder="不填写表示该项保留原状">
            <br>
            <br>
            出版社：<input type="text" id="coursesourcebook-press" value="` + coursesourcebook_array[0].press + `" placeholder="不填写表示该项保留原状">
            <br>
            `)
            for (var i = 1; i < coursesourcebook_array.length; i++) {
                $('#myModal-public-body').append(`
                <br>
                <p  style="color:"blue";" >< ` + parseInt(i + 1) + ` ><a href="javascript:void(0)" id="jcremove` + i + `" value="` + i + `" class="iconfont icon-jian jcremove"></a></p>
                <p id="coursesourcebook-isbn` + i + `">书号：<input type="text" value="` + coursesourcebook_array[i].isbn + `" placeholder="不填写表示该项保留原状"></p>
                <p id="coursesourcebook-name` + i + `">书名：<input type="text" value="` + coursesourcebook_array[i].name + `" placeholder="不填写表示该项保留原状"></p>
                <p id="coursesourcebook-author` + i + `">作者：<input type="text" value="` + coursesourcebook_array[i].author + `" placeholder="不填写表示该项保留原状"></p>
                <p id="coursesourcebook-press` + i + `">出版社：<input type="text" value="` + coursesourcebook_array[i].press + `" placeholder="不填写表示该项保留原状"></p>                
            `)
            }
            $('#myModal-public .modal-footer').html(
                `<button type="button" id="surebtn2" class="btn btn-primary">确认修改</button>
            <button type="button" class="btn btn-primary" id="coursesourcebook-add">继续添加</button>
             <button type="button" class="btn" data-dismiss="modal">关闭</button>
            `
            )
        })

        // ---------------------------------------------------------------
        // 修改课程的名字和类别
        $('#surebtn').click(function () {
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/tea/update-course',
                data: {
                    "cid": courseid_all,
                    "cname": $('#coursename').val(),
                    "ispublic": $('#coursetype').val()
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public h4').html('操作成功')
                        $('#myModal-public-body').html('成功修改课程')
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                        )
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        // 修改大纲的确认按钮
        $('#surebtn1').off('click').click(function () {
            var length = $('.zjremove').size()
            outlines_array = []
            outlines_array = [{
                "id": $('#outline-id').attr('value'),
                "name": $('#outline-name').val(),
                "sectionNum": $('#outline-sectionNum').val(),
                "sectionName": $('#outline-sectionName').val()
            }]
            for (var i = 1; i < length + 1; i++) {
                outlines_array[i] = {
                    "id": $('#outline-id' + [i] + '').attr('value'),
                    "name": $('#outline-name' + [i] + ' input').val(),
                    "sectionNum": $('#outline-sectionNum' + [i] + ' input').val(),
                    "sectionName": $('#outline-sectionName' + [i] + ' input').val()
                }
            }
            // console.log(courseid_all)
            // console.log(outlines_array)
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/tea/upda-cour-ols',
                data: JSON.stringify({
                    "cid": courseid_all,
                    "outlines": outlines_array
                }),
                contentType: 'application/json',
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    console.log(JSON.stringify({
                        "cid": courseid_all,
                        "outlines": outlines_array
                    }))
                    if (data.statusCode == 0) {
                        $('#myModal-public h4').html('操作成功')
                        $('#myModal-public-body').html('成功修改课程大纲')
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                        )
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                        )
                    }
                }
            })
        })
        // 修改教材的确认按钮
        $('#surebtn2').off('click').click(function () {
            var length = $('.jcremove').size()
            coursesourcebook_array = []
            coursesourcebook_array = [{
                "isbn": $('#coursesourcebook-isbn').val(),
                "name": $('#coursesourcebook-name').val(),
                "author": $('#coursesourcebook-author').val(),
                "press": $('#coursesourcebook-press').val()
            }]
            for (var i = 1; i < length + 1; i++) {
                coursesourcebook_array[i] = {
                    "isbn": $('#coursesourcebook-isbn' + [i] + ' input').val(),
                    "name": $('#coursesourcebook-name' + [i] + ' input').val(),
                    "author": $('#coursesourcebook-author' + [i] + ' input').val(),
                    "press": $('#coursesourcebook-press' + [i] + ' input').val()
                }
            }
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/tea/upda-cour-tbs',
                data: JSON.stringify({
                    "cid": courseid_all,
                    "textbooks": coursesourcebook_array
                }),
                contentType: 'application/json',
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    // console.log(data)
                    // console.log(JSON.stringify({
                    //     "cid": courseid_all,
                    //     "textbooks": coursesourcebook_array
                    // }))
                    if (data.statusCode == 0) {
                        $('#myModal-public h4').html('操作成功')
                        $('#myModal-public-body').html('成功修改课程教材')
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                        )
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        // 删除课程
        $('#sureremove').off('click').click(function () {
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/tea/del-course',
                data: {
                    "courseid": remove_courseid
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public h4').html('操作成功')
                        $('#myModal-public-body').html('成功删除课程：' + remove_courseid + '')
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                        )
                        $('#myModal-public').modal({

                        })
                        setTimeout(function () {
                            window.location.href = "course-list.html"
                        }, 1000)
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                        $('#myModal-public .modal-footer').html(
                            `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                        )
                    }
                }
            })
        })
    })

})